package org.woosiyuan.action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class ShowImg extends ActionSupport {

	@Override
	public String execute() throws Exception {
		HttpServletResponse response = (HttpServletResponse) ActionContext
				.getContext().get(ServletActionContext.HTTP_RESPONSE);

		URL aURL;
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		// try {
		aURL = new URL(
				"http://192.168.1.106:81/videostream.cgi?user=admin&password=admin");
		URLConnection conn = aURL.openConnection();
		conn.connect();
		InputStream input = conn.getInputStream();
		System.out.println("input = " + input);
		if (input != null)
			mStopStream = false;
		int readLength = -1;
		String strDate; // 将读取的数据转化为string类型，以便判断包头
		while (!mStopStream) {
			byte[] buffer = new byte[1024];
			readLength = input.read(buffer, 0, 1024);// readLength 本次读取数据的长度
			outStream.write(buffer);
			if (readLength > 0) {
				strDate = new String(buffer, readLength);
				// index标记"Content-Length: "的起始位置
				// index1标记"\r\n"的位置，注意是"Content-Length: "之后的第一个位置
				int index2 = strDate.indexOf(flag2);
				int index = strDate.indexOf(flag);
				int index1 = strDate.indexOf(flag1, index);
				int streamLength = 0;
				if (index != -1 && index1 != -1) {
					// 计算本次streamLength的长度
					streamLength = Integer.parseInt(strDate.substring(index
							+ flag.length(), index1));
				}
				if (streamLength > 0) {
					if ((index1 + 4) < readLength) {
////						String temp = new String("--car \r\n Content-Type: img/jpeg \r\n Content-Length: "+streamLength);
//						outStream.write(buffer,0,index2+19);
//						outStream.write(buffer, index1 + 4, readLength - index1
//								- 4);
						streamLength = streamLength - readLength + index1 + 4;
					}
					// 将剩下读取的视频流存储到buffer1
					byte[] buffer1 = new byte[streamLength];
					int length = 0;
					while (length < streamLength) {
						length += input.read(buffer1, length, streamLength
								- length);

					}
					outStream.write(buffer1,0,streamLength);  // 将剩余的stream写入outStream
					byte[] data = outStream.toByteArray();
					response.addHeader("Accept-Range", "bytes");
					response.addHeader("Cache-Control", "no-cache");
					response.addHeader("Connection", "close");
					response.addHeader("Content-Type", "	multipart/x-mixed-replace;boundary=IPCamera_Logo");
//					response.setCharacterEncoding("utf-8");
//					response.setContentType("image/jpeg");
//					StringBuffer stringBuffer = new StringBuffer();
//					stringBuffer.append("--car \r\n Content-Type: img/jpeg \r\n Content-Length: "+streamLength);
//					stringBuffer.append(new String(data));
					ServletOutputStream servletOutputStream = response.getOutputStream();
					servletOutputStream.write(data);
//					servletOutputStream.flush();
//					response.reset();
					outStream.reset();
				}
			}
		}
		
		return null;
	}

	boolean mStopStream;

	String flag = "Content-Length: ";
	String flag1 = "\r\n";
	String flag2 = "--IPCamera_Logo";

	public void cameraStream() throws IOException {
		URL aURL;
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		// try {
		aURL = new URL(
				"http://192.168.1.106:81/videostream.cgi?user=admin&password=admin");
		URLConnection conn = aURL.openConnection();
		conn.connect();
		InputStream input = conn.getInputStream();
		System.out.println("input = " + input);
		if (input != null)
			mStopStream = false;
		int readLength = -1;
		String strDate; // 将读取的数据转化为string类型，以便判断包头
		while (!mStopStream) {
			byte[] buffer = new byte[1024];
			readLength = input.read(buffer, 0, 1024);// readLength 本次读取数据的长度
			if (readLength > 0) {
				strDate = new String(buffer, readLength);
				// index标记"Content-Length: "的起始位置
				// index1标记"\r\n"的位置，注意是"Content-Length: "之后的第一个位置
				int index = strDate.indexOf(flag);
				int index1 = strDate.indexOf(flag1, index);
				int streamLength = 0;
				if (index != -1 && index1 != -1) {
					// 计算本次streamLength的长度
					streamLength = Integer.parseInt(strDate.substring(index
							+ flag.length(), index1));
				}
				if (streamLength > 0) {
					if ((index1 + 4) < readLength) {
						outStream.write(buffer, index1 + 4, readLength - index1
								- 4);
						streamLength = streamLength - readLength + index1 + 4;
					}
					// 将剩下读取的视频流存储到buffer1
					byte[] buffer1 = new byte[streamLength];
					int length = 0;
					while (length < streamLength) {
						length += input.read(buffer1, length, streamLength
								- length);
					}
					outStream.write(buffer1, 0, streamLength); // 将剩余的stream写入outStream
					byte[] data = outStream.toByteArray();
					File file = new File("a.jpeg");
					FileOutputStream fileOutputStream = new FileOutputStream(
							file);
					fileOutputStream.write(data);
					fileOutputStream.close();
					outStream.reset();
				}
			}
		}
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// try {
		outStream.close();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	} // cameraStream()

}
